package com.rrqs.net;

import android.app.AlertDialog;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.widget.ImageView;

import com.alibaba.fastjson.JSONObject;
import com.litesuits.http.HttpConfig;
import com.litesuits.http.LiteHttp;
import com.litesuits.http.data.NameValuePair;
import com.litesuits.http.exception.HttpException;
import com.litesuits.http.listener.HttpListener;
import com.litesuits.http.log.HttpLog;
import com.litesuits.http.request.AbstractRequest;
import com.litesuits.http.request.BitmapRequest;
import com.litesuits.http.request.FileRequest;
import com.litesuits.http.request.StringRequest;
import com.litesuits.http.request.content.FileBody;
import com.litesuits.http.request.content.HttpBody;
import com.litesuits.http.request.content.JsonBody;
import com.litesuits.http.request.content.multi.MultipartBody;
import com.litesuits.http.request.param.HttpMethods;
import com.litesuits.http.response.Response;
import com.litesuits.http.utils.HttpUtil;
import com.rrqs.util.MyApplication;

import java.io.File;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * 异步请求
 */
public class AsyncHttpClient {

	private LiteHttp httpClient;
	private LiteHttp httpClientHeader;
	private MyApplication appContext;

	public AsyncHttpClient(MyApplication appContext) {

		this.appContext = appContext;
		this.initLiteHttp();
	}
	public AsyncHttpClient(MyApplication appContext, String authorization) {
		this.appContext = appContext;
		List<NameValuePair> headers = new ArrayList<>();
		NameValuePair pair = new NameValuePair("Authorization",authorization);
		headers.add(pair);
		if (httpClientHeader == null) {
			HttpConfig config = new HttpConfig(appContext) // configuration
					.setCommonHeaders(headers)
					.setDebugged(false) // log output when debugged
					.setDetectNetwork(true) // detect network before connect
					.setDoStatistics(true) // statistics of time and traffic
					.setUserAgent("Mozilla/5.0 (...)") // set custom User-Agent
					.setTimeOut(5000, 5000)// connect and socket timeout:
					.setMaxMemCacheBytesSize(1 * 1000 * 1000)// 1M
					.setSocketBufferSize(1 * 1000 * 1000);// 1M

			;
			httpClientHeader = LiteHttp.newApacheHttpClient(config);
		}
	}

	private void initLiteHttp() {
		if (httpClient == null) {
			HttpConfig config = new HttpConfig(appContext) // configuration
															// quickly
					.setDebugged(false) // log output when debugged
					.setDetectNetwork(true) // detect network before connect
					.setDoStatistics(true) // statistics of time and traffic
					.setUserAgent("Mozilla/5.0 (...)") // set custom User-Agent
					.setTimeOut(5000, 5000)// connect and socket timeout:
					.setMaxMemCacheBytesSize(1 * 1000 * 1000)// 1M
					.setSocketBufferSize(1 * 1000 * 1000);// 1M

			;
			httpClient = LiteHttp.newApacheHttpClient(config);
		} else {
			httpClient.getConfig() // configuration directly
					.setDebugged(false) // log output when debugged
					.setDetectNetwork(true) // detect network before connect
					.setDoStatistics(true) // statistics of time and traffic
					.setUserAgent("Mozilla/5.0 (...)") // set custom User-Agent
					.setSocketBufferSize(1 * 1000 * 1000)// 1M
					.setMaxMemCacheBytesSize(1 * 1000 * 1000)// 1M
					.setTimeOut(5000, 5000); // connect and socket timeout:
												// 10s
		}
	}

	public void get(String url, final HttpResponseListener responseListener) {

		final StringRequest request = new StringRequest(url).setMethod(HttpMethods.Get);
		request.setHttpListener(new HttpListener<String>() {
			@Override
			public void onSuccess(String res, Response<String> response) {
				responseListener.onSuccess(response.getHttpStatus().getCode() + "", res);
			}

			@Override
			public void onFailure(HttpException e, Response<String> response) {
				responseListener.onFailure(e);
			}
		});
		httpClient.executeAsync(request);
	}

	/**
	 * 有参数
	 * @param url
	 * @param responseListener
	 */
	public void post(String url, MultipartBody body, final HttpResponseListener responseListener) {

		final StringRequest request = new StringRequest(url).setMethod(HttpMethods.Post);
//		LinkedHashMap<String, String> headers = new LinkedHashMap<String, String>();
//		headers.put("aouth", MainApplication.getInstance().getToken());
//		request.setHeaders(headers);
		request.setHttpBody(body);
		request.setHttpListener(new HttpListener<String>() {
			@Override
			public void onSuccess(String res, Response<String> response) {
				responseListener.onSuccess(response.getHttpStatus().getCode() + "", res);
			}

			@Override
			public void onFailure(HttpException e, Response<String> response) {
				if (e != null && !TextUtils.isEmpty(e.getMessage()) && e.getMessage().contains("Network Is Not Avilable")) {
					responseListener.onSuccess(HttpConstant.HTTP_SUCCESS, "{\"code\": -1, \"message\":\"连接服务器失败，请验证网络情况\"}");
				} else {
					responseListener.onFailure(e);
				}
			}
		});
		httpClient.executeAsync(request);
	}

	/**
	 * 无参数
	 * @param url
	 * @param responseListener
	 */
	public void post(String url, final HttpResponseListener responseListener) {

		final StringRequest request = new StringRequest(url).setMethod(HttpMethods.Post);
//		LinkedHashMap<String, String> headers = new LinkedHashMap<String, String>();
//		headers.put("aouth", MainApplication.getInstance().getToken());
//		request.setHeaders(headers);
		request.setHttpListener(new HttpListener<String>() {
			@Override
			public void onSuccess(String res, Response<String> response) {

				responseListener.onSuccess(response.getHttpStatus().getCode() + "", res);
			}

			@Override
			public void onFailure(HttpException e, Response<String> response) {
				if (e != null && !TextUtils.isEmpty(e.getMessage()) && e.getMessage().contains("Network Is Not Avilable")) {
					responseListener.onSuccess(HttpConstant.HTTP_SUCCESS, "{\"code\": -1, \"message\":\"连接服务器失败，请验证网络情况\"}");
				} else {
					responseListener.onFailure(e);
				}
			}
		});
		httpClient.executeAsync(request);
	}
}
